package com.aos.iot.protocol.hll.parser.data;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.StrUtil;
import com.aos.iot.protocol.hll.constant.HllCmd;
import com.aos.iot.protocol.hll.constant.HllProp;
import com.aos.iot.protocol.hll.util.CodeUtil;
import org.springframework.stereotype.Component;

import java.util.List;

@Component("RebootDataParser")
public class RebootDataParser extends HllDataParser {

    private final List<String> encodeItems = CollUtil.newArrayList(
            HllProp.PILE_NO.getValue(),
            HllProp.PILE_SOFT_VERSION.getValue(),
            HllProp.PILE_CHANNEL_NO.getValue()
    );

    private final List<String> decodeItems = CollUtil.newArrayList(
            HllProp.PILE_NO.getValue(),
            HllProp.PILE_CHANNEL_NO.getValue()
    );

    @Override
    public Dict encode(String payload) {
        return Dict.create()
            .set(HllProp.PILE_NO.getValue(), sub(payload, 0, 10, true, "0000000000"))
            .set(HllProp.PILE_SOFT_VERSION.getValue(), sub(payload, 10, 12, false, "00"))
            .set(HllProp.PILE_CHANNEL_NO.getValue(), sub(payload, 12, 14, false, "00"))
            ;
    }

    @Override
    public List<String> encodeItems() {
        return encodeItems;
    }

    @Override
    public String decode(Dict data) {
        return StrUtil.join("",
            CodeUtil.highRotation(data.getStr(HllProp.PILE_NO.getValue())),
            data.getStr(HllProp.PILE_CHANNEL_NO.getValue())
        );
    }

    @Override
    public List<String> decodeItems() {
        return decodeItems;
    }

    @Override
    public String cmd() {
        return HllCmd.REBOOT;
    }
}
